package com.wxisme.ssm.service.impl;

import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;

import com.wxisme.ssm.mapper.UserInfoMapper;
import com.wxisme.ssm.mapper.UserMapper;
import com.wxisme.ssm.po.User;
import com.wxisme.ssm.po.UserInfo;
import com.wxisme.ssm.service.LoginService;

/**
 *用户管理service
 *@author wxisme
 *@time 2015-8-19 下午6:54:07
 */

public class LoginServiceImpl implements LoginService {
	
	@Autowired
	private UserMapper userMapper;
	
	
	@Autowired
	private UserInfoMapper userInfoMapper;
	
	@Override
	public User findUserById(int id) throws Exception {
		
		return userMapper.findUserById(id);
	}
	
	
	
	
	@Override
	public boolean insertUserByRegister(User user) throws Exception {
		boolean isUserExists = false;
		//校检用户名是否已经存在
		User user_flag = userMapper.findUserByUsername(user.getUsername());
		isUserExists = user_flag==null ? false:true;
		if(!isUserExists) {
			userMapper.insertUserByRegister(user);
		}
		
		//isUserExits为false时用户名可用，插入成功。
		return isUserExists;
	}
	
	
	@Override
	public User findUserByUsername(String username) throws Exception {
		
		User user = userMapper.findUserByUsername(username);
		
		
		return user;
	}
	
	@Override
	public boolean loginCheck(String username, String password)
			throws Exception {
		boolean isMacth = false;
		
		User user = userMapper.findUserByUsername(username);
		
		if(user != null && user.getPassword().equals(password)) {
			isMacth = true;
		}
		
		return isMacth;
	}
	
	
	@Override
	public void insertUserInfoByRegister(UserInfo userInfo) throws Exception {
		
		userInfoMapper.insertUserInfoByRegister(userInfo);
		
		//为注册用户建立文件夹
		String userPath =  
				"/imgdata/" + userInfo.getUserPath();
		System.out.println(userPath);
		
		File userFile = new File(userPath);
		
		userFile.mkdir();
		
		System.out.println(userFile.getAbsolutePath());
		
	}
	

}
